define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'bootstrap-daterangepicker'], function ($, undefined, Backend, Table, Form, undefined) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'semester/index' + location.search,
                    add_url: 'semester/add',
                    edit_url: 'semester/edit',
                    del_url: 'semester/del',
                    multi_url: 'semester/multi',
                    table: 'semester',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'), operate: false},
                        {field: 'school.name', title: __('学校')},
                        {field: 'name', title: __('Name')},
                        {field: 'start_date', title: __('Start_date'), operate: false},
                        {field: 'end_date', title: __('End_date'), operate: false},
                        {field: 'week_nums', title: __('总周数'), operate: false},
                        {
                            field: 'type',
                            title: __('Type'),
                            searchList: {"1": __('Type 1'), "2": __('Type 2')},
                            formatter: Table.api.formatter.normal
                        },
                        {field: 'is_current', title: __('Is_current'), searchList: {"1":__('Yes'),"0":__('No')}, formatter: Table.api.formatter.normal},
                        {
                            field: 'created_at',
                            title: __('Created_at'),
                            sortable: true,
                            formatter: Table.api.formatter.datetime,
                            operate: 'RANGE',
                            addclass: 'datetimerange'
                        },
                        {
                            field: 'updated_at',
                            title: __('Updated_at'),
                            sortable: true,
                            formatter: Table.api.formatter.datetime,
                            operate: 'RANGE',
                            addclass: 'datetimerange'
                        },
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ],
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {

            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                $('.daterange').daterangepicker({
                    timePicker: false,
                    autoUpdateInput: false,
                    timePickerSeconds: true,
                    timePicker24Hour: true,
                    autoApply: true,
                    locale: {
                        format: 'YYYY-MM-DD',
                        customRangeLabel: __("Custom Range"),
                        applyLabel: __("Apply"),
                        cancelLabel: __("Clear"),
                    },
                }, function (start, end) {
                    $(this.element).val(start.format(this.locale.format) + " - " + end.format(this.locale.format));
                    $(this.element).trigger('blur');
                });
                Form.api.bindevent($("form[role=form]"));
            },
        }
    };
    return Controller;
});